package com.vinjogames.gfx;

import javax.microedition.khronos.opengles.GL10;

public class AnimationManager {
	private static int MAX_ANIMATIONS = 50;
	private long[] timers;
	private long[] durations;
	private int[] curFrames;
	private float[] locX;
	private float[] locY;
	private boolean[] loops;
	private int[] animImage;
	private Image[] animations;
	AnimationSheet as = AnimationSheet.getInstance();
	
	private AnimationManager(){};

	private static class AnimationManagerHolder {
		public static final AnimationManager instance = new AnimationManager();
	}

	public static AnimationManager getInstance(){
		return AnimationManagerHolder.instance;
	}
	
	public void initializeManager(int numAnims) {
		timers    = new long[MAX_ANIMATIONS];
		durations = new long[MAX_ANIMATIONS];
		curFrames = new int[MAX_ANIMATIONS];
		loops     = new boolean[MAX_ANIMATIONS];
		locX      = new float[MAX_ANIMATIONS];
		locY      = new float[MAX_ANIMATIONS];
		animImage = new int[MAX_ANIMATIONS];
		animations= new Image[numAnims];
		
		for(int i = 0; i < timers.length; i++) timers[i] = -1;
		
		for(int i = 0; i < animations.length; i++){
			animations[i] = new Image(i,1,1, true);//TODO;  figure out sizes, or howto resize
		}
	}
	

	public void tick(long now){
		if(timers == null) return;
		
		for(int i = 0; i < timers.length; i++){
			if(timers[i] > 0){		
				int numFrames = animations[animImage[i]].getFrames();
				curFrames[i] =  (int)(((now-timers[i]) % (durations[i])) * numFrames/durations[i]);
				
				if(!loops[i]){
					if(now - timers[i]  > durations[i]){
						curFrames[i] = numFrames-1;
					}
				
				}
			}
		}
	}
	
	/**
	 * Starts a new frame animation
	 * @param animation The Image object of the animation
	 * @param duration The duration of the animation
	 * @param loop Whether or not the animation loops
	 * @param x X position in world GL coordinates
	 * @param y Y position in world GL coordinates
	 * @return The timer id
	 */
	public int startFrameAnimation(int animation, long duration, boolean loop, float x, float y, long time) {
		for(int i = 0; i < timers.length; i++){
			if(timers[i] < 0){
				timers[i] = time;
				durations[i] = duration;
				loops[i] = loop;
				locX[i] = x;
				locY[i] = y;
				animImage[i] = animation;
				curFrames[i] = 0;
				return i;
			}
		}
		return -1;
	}
	//TODO:  location animations
	
	public int getCurrentFrame(int animationID){
		int frame = curFrames[animationID];
		if(frame >= animations[animImage[animationID]].getFrames()-1 && !loops[animationID]){
			timers[animationID] = -1;
		}
		return frame;
	}
	
	
	public void clearMemory() {
		if(timers !=null)
			for(int i = 0; i < timers.length; i++) timers[i] = -1;
		timers    = null;
		durations = null;
		curFrames = null;
		loops     = null;
		locX      = null;
		locY      = null;
		animImage = null;
		animations= null;
		
	}
	
	public void draw(GL10 gl){
		if(timers == null) return;
		synchronized(timers){
			for(int i = 0; i < MAX_ANIMATIONS; i++){
				if(timers[i] > 0){
					animations[animImage[i]].draw(gl, locX[i], locY[i], 1,1,1,1,getCurrentFrame(i));
				}
			}
		}
	}
}
 	